import html2canvas from 'html2canvas';

const captureFullPageWithHtml2Canvas = async (): Promise<void> => {
  try {
    await captureScreenshot();
  } catch (error) {
    console.error('截图失败:', error);
    throw error;
  }
};

async function captureScreenshot(): Promise<void> {
  const canvas = await html2canvas(document.body, {
    scrollX: -window.scrollX,
    scrollY: -window.scrollY,
    useCORS: true,
  });

  // JPEG 格式 + 质量压缩（PNG 无质量参数）
  const dataUrl = canvas.toDataURL('image/jpeg', 0.75);
  chrome.runtime.sendMessage(
    { action: 'DOWNLOAD', data: dataUrl },
    function (response) {
      console.log('response: ', response);
    },
  );
}

export default captureFullPageWithHtml2Canvas;
